package com.woniu.tms.userInfo.controller;

import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.User;
import com.woniu.tms.userInfo.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("userInfo")
@Controller
public class UserInfoController {
    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("info/index")
    public String userInfoindex() {
        return "WEB-INF/userInfo";
    }

    @RequestMapping("info/indexDo")
    @ResponseBody
    public Map<String, Object> userInfoIndexDo(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            HttpSession session = request.getSession();
            User loginUser = (User) session.getAttribute("loginUser");

            Classes classes = userInfoService.findByCid(loginUser.getClassid());
            map.put("loginUser", loginUser);
            map.put("classes", classes.getCname());
            map.put("result", true);

        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }

        return map;
    }


    @RequestMapping("info/edit")
    @ResponseBody
    public Map<String, Object> edit(User user) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("*********************");
        System.out.println("UserInfoController.edit()");

        System.out.println(user.getGender());
        System.out.println("*********************");

        try {
            User user2 = userInfoService.findByUid(user.getUid());
            user2.setGender(user.getGender());
            user2.setEducation(user.getEducation());
            user2.setCity(user.getCity());
            ;
            user2.setBirthdate(user.getBirthdate());
            userInfoService.editUser(user2);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }
        return map;
    }

    @RequestMapping("pwd/index")
    public String pwdIndex() {
        return "WEB-INF/editPwd";
    }


    @RequestMapping("pwd/edit")
    public Map<String, Object> pwdCheck(String opwd, String npwd, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
//			HttpSession session=request.getSession();
//			User loginUser=(User) session.getAttribute("loginUser");
            User loginUser = userInfoService.findUserByuid(16);
            if (opwd == loginUser.getUpwd()) {

                if (6 < npwd.length() && npwd.length() < 15) {

                    map.put("msg", "修改成功");
                }

            } else {
                map.put("msg", "旧密码不正确");
            }

        } catch (Exception e) {
            map.put("result", false);
            e.printStackTrace();
        }

        return map;
    }


    @RequestMapping("info/clean")
    public String clean() {
        /*
         *userInfo/inf/index
         * */
        return "";
    }

}
